(() => {
	let ele = {};

	ele.optionDirect = $('#option-all');
	ele.optionIndirect = $('#option-today');
	ele.listDirect = $('#list-all');
	ele.listyestoday = $('#option-yestoday');
	ele.listEmpty = $('#list-empty');
	ele.options = $('#tab-team li');
	ele.real = $('#real');
	ele.mores = $('.more');
	ele.week = $('#week');
	ele.month = $('#month');

	ele.invite_code = $('#invite_code');
	ele.modal_info = $('#modal-info');
	ele.close_info = $('#close-info');

	ele.infoname = $('#infoname');
	ele.infosex = $('#infosex');
	ele.infoid = $('#infoid');
	ele.infowx = $('#infowx');
	ele.copywx = $('#copywx');
	ele.infoqq = $('#infoqq');
	ele.copyqq = $('#copyqq');
	ele.infogroup = $('#infogroup');
	ele.infotxt = $('#infotxt');
	ele.cardBox = $('#cardBox');
	ele.headpic = $('#headpic');
	ele.search = $('#search');
	ele.myinviter = $('#myinviter');
	ele.infoname = $('#infoname');
	ele.infosex = $('#infosex');
	ele.infoid = $('#infoid');
	ele.infowx = $('#infowx');
	ele.copywx = $('#copywx');
	ele.infoqq = $('#infoqq');
	ele.copyqq = $('#copyqq');
	ele.infotxt = $('#infotxt');
	ele.cardBox = $('#cardBox');
	ele.headpic = $('#headpic');
	ele.infogroup2 = $('#infogroup2');
	ele.modal_exchange = $("#modal-exchange");
	ele.close_exchange = $("#close-exchange");
	ele.modal_exchange2 = $("#modal-exchange2");
	ele.close_exchange2 = $("#close-exchange2");
	ele.modal_exchange3 = $("#modal-exchange3");
	ele.close_exchange3 = $("#close-exchange3");
	ele.btn_exchange1 = $("#btn_exchange1");
	ele.btn_exchange2 = $("#btn_exchange2");
	ele.yqd_1 = $("#yqd_1");
	ele.yqd_2 = $("#yqd_2");
	ele.yqd_11 = $("#yqd_11");
	ele.yqd_22 = $("#yqd_22");
	ele.dq_ingerl = $("#dq_ingerl");
	ele.dq_ingerl2 = $("#dq_ingerl2");
	ele.dq_ingerl3 = $("#dq_ingerl3");
	ele.jiannum1 = $("#jiannum1");
	ele.iptnum1 = $("#iptnum1");
	ele.addnum1 = $("#addnum1");
	ele.yuji1 = $("#yuji1");
	ele.queding1 = $("#queding1");
	ele.jiannum2 = $("#jiannum2");
	ele.iptnum2 = $("#iptnum2");
	ele.addnum2 = $("#addnum2");
	ele.yuji2 = $("#yuji2");
	ele.queding2 = $("#queding2");

	
	
	const typeList = ['', 'all','today','yestoday',];
	const urlList = {
		direct: API_PATH.DIRECT_FRIEND,
	};
	let typeName = 'all';
	let currentPage = {
		all: 1,
		today: 0,
		yestoday:2,
	}
	
	// console.log(getCookie("p_invite") || '');
	
	let is_show = getCookie('vip');
	if(is_show == 0) {
		ele.listyestoday.innerHTML = '未激活'
	}else {
		ele.listyestoday.innerHTML = '已激活'
	}
	
	tabBar();
	getFriendList(typeList.indexOf(typeName), currentPage[typeName] , 0);

	// 选项卡选项功能
	function tabBar() {
		for (let i = 0; i < ele.options.length; i++) {
			ele.options[i].addEventListener('click', e => {
				let cls = e.target.className;
				typeName = cls.split(' ')[0].split('-')[1];
				console.log(typeName);
				// if(typeName == 'channel') intil();
				let typeIndex = typeList.indexOf(typeName);
				if (currentPage[typeName] == 0) {
					currentPage[typeName]++;
					ele.listEmpty.style.display = 'none';
					getFriendList2(typeIndex, currentPage[typeName],0);
				} else if (currentPage[typeName] == 2) {
					currentPage[typeName] = 1;
					// ele.listEmpty.style.display = 'none';
					getFriendList3(typeIndex,currentPage[typeName] ,0);
				} else if (currentPage[typeName] == -1) {
					ele.listEmpty.style.display = 'block';
				} else {
					ele.listEmpty.style.display = 'none';
				}
			})
		}
	}

	// 拉取好友列表
	function getFriendList(type, page , invite_code , drawtype) {
		invite_code = invite_code ? invite_code : 0
		let more = ele.mores[type - 1];
		more.innerText = '加载中...';
		let token = getCookie("token");
		let userid = getCookie("userid");
		ajax({
			url: API_PATH.DIRECT_FRIEND,
			method: 'GET',
			header: {
				'Authorization': token,
			},
			data: {
				page: page,
				invite_code:invite_code
			},
			success: res => {
				// console.log(res);
				if (res.code == 1) {
					// ele.week.innerHTML = res.week_num
					// ele.month.innerHTML = res.month_num
					let detail = res.data.data;
					if (detail.length == 0) {
						if(drawtype == 1) {
							let elm = $('#list-' + typeName);
							elm.innerHTML = ''
						}
						currentPage[typeName] = -1;
						more.style.display = "none";
						ele.listEmpty.style.display = "block";
					} else {
						if(drawtype == 1) {
							let elm = $('#list-' + typeName);
							elm.innerHTML = ''
						}
						drawFriendList(detail, res.data.total);
						page++;
						if (page > res.data.last_page) {
							more.innerText = '没有更多了';
							more.onclick = null;
						} else {
							more.innerText = '加载更多';
							more.onclick = () => getFriendList(type, page);
						}
					}
				} else openTips(res.msg);
			}
		});
	}


	// 今日昨日好友
	function getFriendList2(type, page , invite_code , drawtype) {
		invite_code = invite_code ? invite_code : 0
		let more = ele.mores[type - 1];
		// console.log(type)
		more.innerText = '加载中...';
		let token = getCookie("token");
		let userid = getCookie("userid");
		ajax({
			url: API_PATH.INDIRECT_FRIEND,
			method: 'GET',
			header: {
				'Authorization': token,
			},
			data: {
				page: page,
				invite_code:invite_code
			},
			success: res => {
				// console.log(res);
				if (res.code == 1) {
					let detail = res.data.data;
					if (detail.length == 0) {
						if(drawtype == 1) {
							let elm = $('#list-' + typeName);
							elm.innerHTML = ''
						}
						currentPage[typeName] = -1;
						more.style.display = "none";
						ele.listEmpty.style.display = "block";
					} else {
						if(drawtype == 1) {
							let elm = $('#list-' + typeName);
							elm.innerHTML = ''
						}
						drawFriendList(detail, res.data.total);
						page++;
						if (page > res.data.last_page) {
							more.innerText = '没有更多了';
							more.onclick = null;
						} else {
							more.innerText = '加载更多';
							more.onclick = () => getFriendList2(type, page);
						}
					}
				} else openTips(res.msg);
			}
		});
	}
	
	// 渠道好友
	function getFriendList3(type, page , invite_code , drawtype) {
		invite_code = invite_code ? invite_code : 0
		let more = ele.mores[type - 1];
		// console.log(type)
		more.innerText = '加载中...';
		let token = getCookie("token");
		let userid = getCookie("userid");
		ajax({
			url: API_PATH.USER_GET_QUDAO_USERL,
			method: 'GET',
			header: {
				'Authorization': token,
			},
			data: {
				page: page,
				invite_code:invite_code
			},
			success: res => {
				// console.log(res);
				if (res.code == 1) {
					let detail = res.data.data;
					if (detail.length == 0) {
						if(drawtype == 1) {
							let elm = $('#list-' + typeName);
							elm.innerHTML = ''
						}
						currentPage[typeName] = -1;
						more.style.display = "none";
						ele.listEmpty.style.display = "block";
					} else {
						if(drawtype == 1) {
							let elm = $('#list-' + typeName);
							elm.innerHTML = ''
						}
						drawFriendList(detail, res.data.total,3);
						page++;
						if (page > res.data.last_page) {
							more.innerText = '没有更多了';
							more.onclick = null;
						} else {
							more.innerText = '加载更多';
							more.onclick = () => getFriendList3(type, page);
						}
					}
				} else openTips(res.msg);
			}
		});
	}

	// 渲染好友列表
	function drawFriendList(data, total,type) {
		let imgh = 'http://img.qxgs.cc/tiantianlaishouzu/img/qudao/hb.png';
		if(type == 3) {
			imgh = '../img/each_Fang/MG_YQD.png';
		}
		// console.log(data,total);
		if(data == ''|| data == null){
			ele.listDirect.style.border = 'none';
			ele.listDirect.style.boxShadow = 'none';
		}
		// if (typeName == 'all') ele.optionDirect.innerText = '我的好友(' + total + ')';
		// else if(typeName == 'today') ele.optionIndirect.innerText = '今日邀请好友(' + total + ')';
		// else ele.listyestoday.innerText = '昨日邀请好友(' + total + ')';
		
		let elm = $('#list-' + typeName);
		let html = '';
		for (let i = 0; i < data.length; i++) {
			let d = data[i];
			let cls = d.is_check == 1 ? 'active' : '';
			let real = d.is_check == 1 ? '已实名' : '未实名';
			let head_pic = '';
			let nick_name = '';
			// console.log(data[i].detail);
			if(data[i].detail != null) {
				nick_name = data[i].detail.nick_name
				if (data[i].detail.head_pic != null) {
					head_pic = IMG_DOMAIN + data[i].detail.head_pic
				} else {
					if (data[i].detail.sex == 1) {
						head_pic = 'http://img.qxgs.cc/tiantianlaishouzu/img/my/tx1.png'
					} else {
						head_pic = 'http://img.qxgs.cc/tiantianlaishouzu/img/my/tx2.png'
					}
				}
			}else {
				head_pic = 'http://img.qxgs.cc/tiantianlaishouzu/img/my/tx1.png'
				nick_name = '暂未设置';
			}
			html += '<div class="item">'+
						'<div class="left">'+
							'<img src="'+head_pic+'">'+
							'<div class="right">'+
								'<p class="mobile">'+nick_name+'</p>'+
								'<p class="time">ID：'+d.invite_code+'</p>'+
							'</div>'+
						'</div>'+
						'<div class="real">'+
							'<div class="realin">'+
								'<p>今日奖励</p>'+
								'<div class="jinri">'+
									'<img src="'+imgh+'" alt="">'+
									'<span>'+d.money+'</span>'+
								'</div>'+
							'</div>'+
							'<div class="realin">'+
								'<p>累计奖励</p>'+
								'<div class="jinri">'+
									'<img src="'+imgh+'" alt="">'+
									'<span>'+d.all_money+'</span>'+
								'</div>'+
							'</div>'+
						'</div>'+
					'</div>';
		}
		elm.innerHTML += html;
		
		let items = $('.item');
		for ( i = 0; i < items.length; i ++) {
			items[items.length - 1].style.border = 'none';
		}
	}
	
	// 打开我的邀请人弹窗
	ele.myinviter.onclick = ()=>{
		let p_invite = getCookie("p_invite" || '');
		// console.log(getCookie("p_invite"));
		 searchUser(p_invite);
		// ele.modal_info.style.display = 'block'
	}

	// 关闭我的邀请人弹窗
	ele.close_info.onclick = ()=>{
		ele.modal_info.style.display = 'none'
	}

	// 搜索我的好友
	ele.search.onclick = ()=>{
		let invite_code = ele.invite_code.value
		if(typeName == 'all') {
			getFriendList(typeList.indexOf(typeName), 1 , invite_code , 1);
		}else if(typeName == 'today') {
			getFriendList2(typeList.indexOf(typeName), 1 , invite_code , 1);
		}else if(typeName == 'yestoday') {
			getFriendList3(typeList.indexOf(typeName), 1 , invite_code , 1);
		}
	}

	// 检索用户  我的邀请人
	function searchUser(p_invite) {
		let token = getCookie("token");
		ajax({
			url: API_PATH.SEARCH_USER,
			method: 'GET',
			header: {
				'Authorization': token,
			},
			data: {
				code: p_invite
			},
			success: res => {
				if (res.code == 1) {
					ele.infoname.innerHTML = res.data.nick_name
					let sex = res.data.sex == 1 ? '男' : '女'
					let age = res.data.age == null ? '0' : res.data.age
					ele.infosex.innerHTML = '性别：'+sex+'    年龄：'+ age;
					ele.infoid.innerHTML = 'ID:' +res.data.invite_code;
					ele.infowx.innerHTML = res.data.wechat_no == null || res.data.wechat_no == '' ? '微信号暂未设置' : res.data.wechat_no;
					ele.infoqq.innerHTML = res.data.infoqq == null || res.data.infoqq == '' ? 'QQ号暂未设置' : res.data.infoqq;
					ele.infogroup.src = IMG_DOMAIN + res.data.qrcode;
					if(res.data.qrcode == null) {
						ele.infogroup.style.display = 'none'
						ele.infogroup2.style.display = 'block'
					} else {
						ele.infogroup.style.display = 'block'
						ele.infogroup2.style.display = 'none'
					}
					ele.infotxt.innerHTML = res.data.introduce == null  || res.data.introduce == '' ? 'ta很懒，什么都没有介绍~' : res.data.introduce;
					if(res.data.head_pic == null) {
						ele.headpic.src = res.data.sex == 1 ? 'http://img.qxgs.cc/tiantianlaishouzu/img/my/tx1.png' : 'http://img.qxgs.cc/tiantianlaishouzu/img/my/tx2.png'
					}else {
						ele.headpic.src = IMG_DOMAIN + res.data.head_pic;
					}
					openMdl(ele.modal_info);
					ele.copywx.onclick = ()=> {
						copy(ele.infowx.innerHTML)
						if(ele.infowx.innerHTML) {
							openTips('复制成功~')
						} else {
							openTips('复制失败，请手动复制~')
						}
					}
					ele.copyqq.onclick = ()=> {
						copy(ele.infoqq.innerHTML)
						if(ele.infoqq.innerHTML) {
							openTips('复制成功~')
						} else {
							openTips('复制失败，请手动复制~')
						}
					}
				} else openTips(res.msg || '未知错误');
			}
		});
	}


	// 我的邀请人


	// ele.infoname.innerHTML = res.data.nick_name
	// ele.infosex.innerHTML = '性别：'+res.data.sex+'    年龄：'+res.data.age; 
	// ele.infoid.innerHTML = 'ID:' + res.data.id;
	// ele.infowx.innerHTML = res.data.wechat_no == null || res.data.wechat_no == '' ? '微信号暂未设置' : res.data.wechat_no;
	// ele.infoqq.innerHTML = res.data.infoqq == null || res.data.infoqq == '' ? 'QQ号暂未设置' : res.data.infoqq;
	// ele.infogroup.src = IMG_DOMAIN + res.data.qrcode;
	// ele.infotxt.innerHTML = res.data.introduce == null  || res.data.introduce == '' ? 'ta很懒，什么都没有介绍~' : res.data.introduce;
	// ele.headpic.src = IMG_DOMAIN + res.data.head_pic;
	// for(let i = 0; i < res.data.house_list.length; i ++) {
	// 	ele.cardBox.innerHTML += '<div class="infoCard">'
	// 								+'<div class="card1">'
	// 									+'<img src="http://img.qxgs.cc/tiantianlaishouzu/img/home/fcz.png" alt="">'
	// 									+'<span>X'+res.data.house_list[i].num+'</span>'
	// 								+'</div>'
	// 								+'<p class="card2">'+res.data.house_list[i].name+'</p>'
	// 							+' </div>';
	// 				}


	// 赵强强
	ele.close_exchange.onclick = () => {
		closeMdl(ele.modal_exchange);
	}
	ele.close_exchange2.onclick = () => {
		closeMdl(ele.modal_exchange2);
	}
	ele.close_exchange3.onclick = () => {
		closeMdl(ele.modal_exchange3);
	};
	ele.btn_exchange1.onclick = () => {
		openMdl(ele.modal_exchange);
	};
	ele.btn_exchange2.onclick = () => {
		openMdl(ele.modal_exchange2);
	};
	
	intergalList();
	
	let rate1 = '';
	let rate2 = '';
	let num1 = '';
	let num2 = '';
	let my_yqd = '';
	
	// 列表的接口 get 友情点兑换
	function intergalList() {
		let token = getCookie("token");
		ajax({
			method: "get",
			url: API_PATH.INTEGRAL_LIST,
			header: {
				Authorization: token,
			},
			data: {
				
			},
			success: (res) => {
				if(res.code == 1) {
					rate1 = 1 / res.data.data.zt_recharge_reward
					num1 = res.data.data.zt_recharge_reward
					rate2 = 0.1 / res.data.data.jt_recharge_reward
					num2 = res.data.data.jt_recharge_reward
					ele.yqd_1.innerHTML = res.data.data.zt_recharge_reward + '友情点'
					ele.yqd_2.innerHTML = res.data.data.jt_recharge_reward + '友情点'
					ele.yqd_11.innerHTML = res.data.data.zt_recharge_reward + '友情点'
					ele.yqd_22.innerHTML = res.data.data.jt_recharge_reward + '友情点'
					my_yqd = res.data.detail.integral
					ele.dq_ingerl.innerHTML = res.data.detail.integral
					ele.dq_ingerl2.innerHTML = res.data.detail.integral
					ele.dq_ingerl3.innerHTML = res.data.detail.integral
					ele.yuji1.innerHTML = Math.floor(num1 * rate1 * 10) / 10
					ele.yuji2.innerHTML = Math.floor(num2 * rate2 * 10) / 10
				}
			},
		});
	}
	
	ele.iptnum1.oninput = ()=> {
		// console.log(rate1,rate2);
		let num = ele.iptnum1.value;
		if(parseInt(num) < 0) {
			ele.iptnum1.value = 0;
			ele.yuji1.innerHTML = 0
		}else if(parseInt(num) > parseInt(my_yqd)) {
			ele.iptnum1.value = my_yqd
			ele.yuji1.innerHTML = Math.floor(ele.iptnum1.value * rate1 * 10) / 10
		}else if(!/^\\d+$/.test(ele.iptnum1.value)) {
			ele.iptnum1.value = parseInt(ele.iptnum1.value)
			ele.yuji1.innerHTML = Math.floor(num * rate1 * 10) / 10
		}
		
	}
	
	ele.jiannum1.onclick = ()=> {
		let num = ele.iptnum1.value;
		num = parseInt(num) - parseInt(num1);
		if(num < 0) {
			openTips('请输入正确的兑换数量')
			ele.iptnum1.value = 0;
			ele.yuji1.innerHTML = Math.floor(num * rate1 * 10) / 10
		}else {
			ele.iptnum1.value = num;
			ele.yuji1.innerHTML = Math.floor(num * rate1 * 10) / 10
		}
	}
	
	ele.addnum1.onclick = ()=> {
		let num = ele.iptnum1.value || 0;
		num = parseInt(num) + parseInt(num1);
		ele.iptnum1.value = num;
		ele.yuji1.innerHTML = Math.floor(num * rate1 * 10) / 10
	}
	
	ele.queding1.onclick = ()=> {
		let num = ele.iptnum1.value || 0;
		console.log(num,my_yqd);
		if(num <= 0) {
			openTips('请输入正确的兑换数量')
		}
		else if(parseInt(num) > parseInt(my_yqd)) {
			openTips('友情点不足')
		} 
		else {
			integralExchange(1,num)
		}
	}
	
	ele.iptnum2.oninput = ()=> {
		// console.log(rate1,rate2);
		let num = ele.iptnum2.value;
		if(parseInt(num) < 0) {
			ele.iptnum2.value = 0;
			ele.yuji2.innerHTML = 0
		}else if(parseInt(num) > parseInt(my_yqd)) {
			ele.iptnum2.value = my_yqd
			ele.yuji2.innerHTML =  Math.floor(ele.iptnum2.value * rate2 * 10) / 10
		}else if(!/^\\d+$/.test(ele.iptnum2.value)) {
			ele.iptnum2.value = parseInt(ele.iptnum2.value)
			ele.yuji2.innerHTML =  Math.floor(num * rate2 * 10) / 10
		}
		
	}
	
	ele.jiannum2.onclick = ()=> {
		let num = ele.iptnum2.value || 0;
		num = parseInt(num) - parseInt(num2);
		if(num < 0) {
			openTips('请输入正确的兑换数量')
			ele.iptnum2.value = 0;
			ele.yuji2.innerHTML = Math.floor(num * rate2 * 10) / 10
		}else {
			ele.iptnum2.value = num;
			ele.yuji2.innerHTML = Math.floor(num * rate2 * 10) / 10
		}
	}
	
	ele.addnum2.onclick = ()=> {
		let num = ele.iptnum2.value || 0;
		num = parseInt(num) + parseInt(num2);
		ele.iptnum2.value = num;
		ele.yuji2.innerHTML = Math.floor(num * rate2 * 10) / 10
	}
	
	ele.queding2.onclick = ()=> {
		let num = ele.iptnum2.value || 0;
		if(num <= 0) {
			openTips('请输入正确的兑换数量')
		}
		else if(parseInt(num) > parseInt(my_yqd)) {
			openTips('友情点不足')
		} 
		else {
			integralExchange(2,num)
		}
	}
	
	// 列表的接口 get 友情点兑换
	function integralExchange(type,num) {
		let token = getCookie("token");
		ajax({
			method: "post",
			url: API_PATH.INTEGRAL_EXCHANGE,
			header: {
				Authorization: token,
			},
			data: {
				type: type,
				num: num
			},
			success: (res) => {
				if(res.code == 1) {
					openTips(res.msg)
					intergalList();
				}else {
					openTips(res.msg || '未知错误')
				}
			},
		});
	}
	

})();
